home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 52 / quiksort / quiksort.lst < prev   
File List  |  1987-04-02  |  3KB  |  126 lines

  1. '
  2. '                    Arrays To Be Dimensioned For QuickSort
  3. '
  4. Dim A$(Xq%)                       ! String Array To Be Sorted
  5. Dim Sort$(Xq%)                    ! Temporary String Array used By QuickSort
  6. Dim Sort%(Xq%)                    ! Index Array
  7. Dim Pq%(20),Wq%(20)               ! Arrays Used By QuickSort
  8. '
  9. '               Copy String Array to be Sorted in Temporary Array
  10. '
  11. For I%=0 To Xq%                ! Xq%       = Number Of Elements In Array
  12.   Sort%(I%)=I%                 ! Initialize Index Array
  13.   Sort$(I%)=A$(I%)             ! Sort$()   = Tempoary Sort String
  14.   '                            ! A$()      = String Array To Be Sorted
  15. Next I%
  16. '
  17. '                         Initialize Quick Sort Variables
  18. '
  19. Kq%=1
  20. Pq%(Kq%)=0                             ! Start Count Of Array
  21. Wq%(Kq%)=Xq%                           ! End   Count Of Array
  22. Dq%=0                                  ! Start Count Of Array
  23. Rq%=Xq%                                ! End   Count Of Array
  24. '
  25. ' ---------------------------- QUICK SORT ALGORITHM ----------------------------
  26. Point_1:
  27. If Rq%-Dq%<9
  28.   Goto Point_10
  29. Endif
  30. Iq%=Dq%
  31. Jq%=Rq%
  32. ' ------------------------------------------------------------------------------
  33. Point_2:
  34. If Sort$(Iq%)>Sort$(Jq%)
  35.   Goto Point_5
  36. Endif
  37. ' ------------------------------------------------------------------------------
  38. Point_3:
  39. Dec Jq%
  40. If Jq%>Iq%
  41.   Goto Point_2
  42. Endif
  43. Inc Jq%
  44. ' ------------------------------------------------------------------------------
  45. Point_4:
  46. Inc Kq%
  47. If (Iq%-Dq%)<(Rq%-Jq%)
  48.   Goto Point_9
  49. Endif
  50. Pq%(Kq%)=Dq%
  51. Wq%(Kq%)=Iq%
  52. Dq%=Jq%
  53. Goto Point_1
  54. ' ------------------------------------------------------------------------------
  55. Point_5:
  56. Tq$=Sort$(Jq%)
  57. Tq%=Sort%(Jq%)
  58. Sort$(Jq%)=Sort$(Iq%)
  59. Sort%(Jq%)=Sort%(Iq%)
  60. Sort$(Iq%)=Tq$
  61. Sort%(Iq%)=Tq%
  62. Goto Point_7
  63. ' ------------------------------------------------------------------------------
  64. Point_6:
  65. If Sort$(Jq%)<Sort$(Iq%)
  66.   Goto Point_8
  67. Endif
  68. ' ------------------------------------------------------------------------------
  69. Point_7:
  70. Inc Iq%
  71. If Jq%>Iq%
  72.   Goto Point_6
  73. Endif
  74. Inc Jq%
  75. Goto Point_4
  76. ' ------------------------------------------------------------------------------
  77. Point_8:
  78. Tq$=Sort$(Jq%)
  79. Tq%=Sort%(Jq%)
  80. Sort$(Jq%)=Sort$(Iq%)
  81. Sort%(Jq%)=Sort%(Iq%)
  82. Sort$(Iq%)=Tq$
  83. Sort%(Iq%)=Tq%
  84. Goto Point_3
  85. ' ------------------------------------------------------------------------------
  86. Point_9:
  87. Pq%(Kq%)=Jq%
  88. Wq%(Kq%)=Rq%
  89. Rq%=Iq%
  90. Goto Point_1
  91. ' ------------------------------------------------------------------------------
  92. Point_10:
  93. If (Rq%-Dq%+1)=1
  94.   Goto Point_11
  95. Endif
  96. For Iq%=Dq%+1 To Rq%
  97.   For Jq%=Dq% To Iq%-1
  98.     Cq%=Iq%-Jq%+Dq%-1
  99.     If Sort$(Cq%)<=Sort$(Cq%+1)
  100.       Jq%=Iq%-1
  101.       Goto Drop_out
  102.     Endif
  103.     Tq$=Sort$(Cq%)
  104.     Tq%=Sort%(Cq%)
  105.     Sort$(Cq%)=Sort$(Cq%+1)
  106.     Sort%(Cq%)=Sort%(Cq%+1)
  107.     Sort$(Cq%+1)=Tq$
  108.     Sort%(Cq%+1)=Tq%
  109.     Drop_out:
  110.   Next Jq%
  111. Next Iq%
  112. ' ------------------------------------------------------------------------------
  113. Point_11:
  114. Dq%=Pq%(Kq%)
  115. Rq%=Wq%(Kq%)
  116. Dec Kq%
  117. If Kq%=0
  118.   Goto Sort_done
  119. Endif
  120. Goto Point_1
  121. ' ------------------------------------------------------------------------------
  122. Sort_done:
  123. '
  124. '                         Sort%() Contains an Index to A$()
  125. '
  126.